package com.ruoyi.web.controller.report;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @version v1.0
 * @ProjectName: report
 * @ClassName: ApiTransitController
 * @Description: api透传controller
 * @Author: sunyan
 * @Date: 2022/3/8 14:18
 */
@RestController
@RequestMapping("/report/transit")
public class ApiTransitController {
    @Autowired
    private RestTemplate restTemplate;

    @ApiOperation(value = "接口透传", response = AjaxResult.class)
    @PostMapping(value = "api")
    public AjaxResult transit(@RequestBody JSONObject object) {

        //方法类型
        String method = object.getString("method");
        //请求地址
        String url = object.getString("url");
        //参数
        JSONObject query  = object.getJSONObject("query");

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        //请求方法体
        HttpMethod httpMethod = HttpMethod.GET;
        HttpEntity<JSONObject> entity = new HttpEntity<>(query, headers);

        if("GET".equals(method)){
            if (query != null){
                url = url + "?";
                for(String key : query.keySet()){
                    url = url + key + "=" + query.get(key) + "&";
                }
                url = StringUtils.substring(url,0,-1);
            }
            try {
                ResponseEntity<JSONObject> responseData = restTemplate.exchange(url, httpMethod, null, JSONObject.class);
                if (responseData.getBody() != null) {
                    JSONObject jsonObject = responseData.getBody();
                    if (jsonObject.get("data") != null) {
                        return AjaxResult.success(jsonObject.get("data"));
                    } else {
                        return AjaxResult.error("请检查接口地址和参数是否正确");
                    }

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else if("POST".equals(method)) {
            httpMethod = HttpMethod.POST;
            try {
                ResponseEntity<JSONObject> responseData = restTemplate.exchange(url, httpMethod, entity, JSONObject.class);

                if (responseData.getBody() != null) {

                    JSONObject jsonObject = responseData.getBody();
                    if (jsonObject.get("data") != null) {
                        return AjaxResult.success(jsonObject.get("data"));
                    } else {
                        return AjaxResult.error("请检查接口地址和参数是否正确");
                    }

                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return AjaxResult.success();

    }
}
